﻿@page "/productionorders/index"

@attribute [Authorize]
@inject IProductionOrderService ProductionOrderService
@inject DialogService DialogService
@inject ExampleJsInterop JsInterop
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject IAuthorizationService AuthorizationService

<RadzenCard Variant="Variant.Filled" Style="height : 100%;">
    <RadzenStack>
        <SearchFormPartial Model="_searchRequestDto" QueryHandler="QueryHandler"></SearchFormPartial>

        <SearchTablePartial @ref="_requestTable" T="ProductionOrderInfoResponseDto" ServerData="ServerReload"
                            SearchPartial="_searchPartial" AddHandler="AddHandler"
                            EditHandler="EditHandler">
        </SearchTablePartial>
    </RadzenStack>
</RadzenCard>

@code {
    private ProductionOrderSearchRequestDto _searchRequestDto = new();
    private IEnumerable<ProductionOrderInfoResponseDto> _pagedData = new List<ProductionOrderInfoResponseDto>();
    private SearchPartialInfo _searchPartial = new();
    private SearchTablePartial<ProductionOrderInfoResponseDto> _requestTable = new();
    private int _totalItems;

    async Task QueryHandler(MouseEventArgs args)
    {
        await _requestTable.ReloadServerData();
    }

    async Task AddHandler(MouseEventArgs args)
    {
        await DialogService.OpenAsync<ProductionOrderEdit>("Add Production Order", null);
        await _requestTable.ReloadServerData();
    }

    async Task EditHandler(MouseEventArgs args, ProductionOrderInfoResponseDto context)
    {
        await DialogService.OpenAsync<ProductionOrderEdit>("Edit Production Order", new Dictionary<string, object> { { "Key", context.Id } });
        await _requestTable.ReloadServerData();
    }

    // async Task DownloadExcelHandler()
    // {
    //     var response = await ProductionOrderService.ExportTechnologies(_searchRequestDto);
    //     var fileName = HttpResponseMessageHelper.GetFileName(response);
    //     var stream = await response.Content.ReadAsStreamAsync();
    //     await JsInterop.DownloadFileFromStream(stream, fileName);
    // }

    /// <summary>
    /// Here we simulate getting the paged, filtered and ordered data from the server
    /// </summary>
    async Task<ODataServiceResult<ProductionOrderInfoResponseDto>> ServerReload(LoadDataArgs state)
    {
        if (state.Skip != null) _searchRequestDto.Page = (int)(state.Skip + 1);
        if (state.Top != null) _searchRequestDto.Limit = (int)state.Top;
        var data = await ProductionOrderService.QueryProductionOrdersByPage(_searchRequestDto);

        if (data != null)
        {
            _pagedData = data.Data;
            _totalItems = data.TotalCount;
        }

        return new ODataServiceResult<ProductionOrderInfoResponseDto>() { Count = _totalItems, Value = _pagedData };
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        //var authenticationState = await AuthenticationStateProvider.GetAuthenticationStateAsync();

        _searchPartial.IsAdd = true;
        _searchPartial.IsEdit = true;
        _searchPartial.IsDelete = true;
        // _searchPartial.IsImportExcel = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.ProductionOrder.ImportProductionOrder))).Succeeded;
        // _searchPartial.IsExportExcel = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.ProductionOrder.ExportProductionOrder))).Succeeded;
    }

}